package com.ruoyi.project.modules.businessSystem.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.project.modules.category.domain.Category;
import com.ruoyi.project.modules.category.service.ICategoryService;
import com.ruoyi.project.modules.interfaceObject.domain.InterfaceObject;
import com.ruoyi.project.modules.interfaceObject.service.IInterfaceObjectService;
import com.ruoyi.project.modules.interfaceinfo.domain.VirtualUsage;
import com.ruoyi.project.modules.interfaceinfo.service.IVirtualUsageService;
import com.ruoyi.project.modules.table.domain.TableInfo;
import com.ruoyi.project.modules.table.service.ITableInfoService;
import com.ruoyi.project.modules.tableObject.domain.TableObject;
import com.ruoyi.project.modules.tableObject.service.ITableObjectService;
import com.ruoyi.project.modules.usage.domain.Usage;
import com.ruoyi.project.modules.usage.domain.UsageParticipateRole;
import com.ruoyi.project.modules.usage.service.IUsageService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.modules.businessSystem.domain.BusinessSystem;
import com.ruoyi.project.modules.businessSystem.service.IBusinessSystemService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 业务系统Controller
 *
 * @author smallrain
 * @date 2021-11-06
 */
@Controller
@RequestMapping("/businessSystem/businessSystem")
public class BusinessSystemController extends BaseController {
    private String prefix = "businessSystem/businessSystem";

    @Autowired
    private IBusinessSystemService businessSystemService;

    @RequiresPermissions("businessSystem:businessSystem:view")
    @GetMapping()
    public String businessSystem() {
        return prefix + "/businessSystem";
    }

    /**
     * 查询业务系统列表
     */
    @RequiresPermissions("businessSystem:businessSystem:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BusinessSystem businessSystem) {
        startPage();
        List<BusinessSystem> list = businessSystemService.selectBusinessSystemList(businessSystem);
        return getDataTable(list);
    }

    /**
     * 导出业务系统列表
     */
    @RequiresPermissions("businessSystem:businessSystem:export")
    @Log(title = "业务系统", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BusinessSystem businessSystem) {
        List<BusinessSystem> list = businessSystemService.selectBusinessSystemList(businessSystem);
        ExcelUtil<BusinessSystem> util = new ExcelUtil<BusinessSystem>(BusinessSystem.class);
        return util.exportExcel(list, "业务系统数据");
    }

    @Autowired
    private ITableInfoService tableInfoService;
    /**
     * 新增业务系统
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        List<TableInfo> tableInfos = tableInfoService.selectTableInfoList(new TableInfo());
        mmap.put("tableList", tableInfos);
        return prefix + "/add";
    }

    /**
     * 新增保存业务系统
     */
    @RequiresPermissions("businessSystem:businessSystem:add")
    @Log(title = "业务系统", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BusinessSystem businessSystem) {
        return toAjax(businessSystemService.insertBusinessSystem(businessSystem));
    }

    @Autowired
    private ITableObjectService tableObjectService;

    @Autowired
    private ICategoryService categoryService;

    /**
     * 修改业务系统
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        BusinessSystem businessSystem = businessSystemService.selectBusinessSystemById(id);
        List<TableInfo> tableInfos = tableInfoService.selectTableInfoList(new TableInfo());
        TableObject tableObject = new TableObject();
        tableObject.setObjectId(id);
        tableObject.setObjectType("1");
        List<TableObject> tableObjects = this.tableObjectService.selectTableObjectList(tableObject);
        if (CollectionUtil.isNotEmpty(tableObjects) && CollectionUtil.isNotEmpty(tableInfos)) {
            List<Long> tableIds = new ArrayList<>();
            for (TableObject object : tableObjects) {
                tableIds.add(object.getTableId());
            }
            mmap.put("tableIds", tableIds);
        }
        mmap.put("tableList", tableInfos);
        if (businessSystem.getCategoryId() != null) {
            Category category = categoryService.selectCategoryById(businessSystem.getCategoryId());
            businessSystem.setCategoryName(category.getCategoryName());
        }
        mmap.put("businessSystem", businessSystem);
        return prefix + "/edit";
    }

    /**
     * 修改保存业务系统
     */
    @RequiresPermissions("businessSystem:businessSystem:edit")
    @Log(title = "业务系统", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BusinessSystem businessSystem) {
        return toAjax(businessSystemService.updateBusinessSystem(businessSystem));
    }

    /**
     * 删除业务系统
     */
    @RequiresPermissions("businessSystem:businessSystem:remove")
    @Log(title = "业务系统", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(businessSystemService.deleteBusinessSystemByIds(ids));
    }

    @Autowired
    private IInterfaceObjectService interfaceObjectService;

    @Autowired
    private IVirtualUsageService virtualUsageService;

    @Autowired
    private IUsageService usageService;

    @GetMapping("/usageList")
    public String usageList(@RequestParam Long id, ModelMap mmap) {
        BusinessSystem businessSystem = this.businessSystemService.selectBusinessSystemById(id);
        mmap.put("businessSystem", businessSystem);
        TableObject tableObject = new TableObject();
        tableObject.setObjectId(id);
        tableObject.setObjectType("1");
        // 先查表
        List<TableObject> tableObjects = this.tableObjectService.selectTableObjectList(tableObject);
        List<Usage> usageList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(tableObjects)) {
            List<Long> tableIds = tableObjects.stream().map(a -> a.getTableId()).collect(Collectors.toList());
            // 根据表查dubbo
            InterfaceObject interfaceObject = new InterfaceObject();
            interfaceObject.setTableIds(tableIds);
            interfaceObject.setObjectType("1");
            List<InterfaceObject> interfaceObjectList = this.interfaceObjectService.selectInterfaceObjectList(interfaceObject);
            if (CollectionUtil.isNotEmpty(interfaceObjectList)) {
                Set<Long> dubboInterfaceIds = interfaceObjectList.stream().map(a -> a.getInterfaceId()).collect(Collectors.toSet());
                VirtualUsage virtualUsage = new VirtualUsage();
                virtualUsage.setDubboInterfaceIds(dubboInterfaceIds);
                // 查询bff接口
                List<VirtualUsage> virtualUsages = this.virtualUsageService.selectVirtualUsageList(virtualUsage);
                if (CollectionUtil.isNotEmpty(virtualUsages)) {
                    Set<Long> bffInterfaceIds = virtualUsages.stream().map(a -> a.getRelationInterfaceId()).collect(Collectors.toSet());
                    // 查询用况数据
                    usageList = this.usageService.selectUsageListByBff(bffInterfaceIds);
                }
            }
        }
        mmap.put("usageList", usageList);
        return prefix + "/usageList";
    }


    @GetMapping("/detail/{id}")
    public String details(@PathVariable("id") Long id, ModelMap mmap) {
        BusinessSystem businessSystem = this.businessSystemService.selectBusinessSystemById(id);
        if (businessSystem.getCategoryId() != null) {
            Category category = categoryService.selectCategoryById(businessSystem.getCategoryId());
            businessSystem.setCategoryName(category.getCategoryName());
        }
        mmap.put("businessSystem", businessSystem);
        return prefix + "/details";
    }

    @PostMapping("/usageList")
    @ResponseBody
    public AjaxResult usageList(@RequestParam Long businessId) {
        TableObject tableObject = new TableObject();
        tableObject.setObjectId(businessId);
        tableObject.setObjectType("1");
        // 先查表
        List<TableObject> tableObjects = this.tableObjectService.selectTableObjectList(tableObject);
        List<Usage> usageList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(tableObjects)) {
            List<Long> tableIds = tableObjects.stream().map(a -> a.getTableId()).collect(Collectors.toList());
            // 根据表查bff
            Set<Long> bffIds = this.businessSystemService.bffList(tableIds);
            if (CollectionUtil.isNotEmpty(bffIds)) {
                InterfaceObject interfaceObject = new InterfaceObject();
                interfaceObject.setBffIds(bffIds);
                interfaceObject.setObjectType("3");
                List<InterfaceObject> interfaceObjectList = this.interfaceObjectService.selectInterfaceObjectList(interfaceObject);
                if (CollectionUtil.isNotEmpty(interfaceObjectList)) {
                    Set<Long> usageIds = interfaceObjectList.stream().map(InterfaceObject::getObjectId).collect(Collectors.toSet());
                    if (CollectionUtil.isNotEmpty(usageIds)) {
                        Usage usage = new Usage();
                        usage.setUsageIds(usageIds);
                        usageList = this.usageService.selectUsageList(usage);
                        return AjaxResult.success(usageList);
                    }
                }
            }
        }
        return AjaxResult.success(new ArrayList<Usage>());
    }
}
